<!--
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2022-04-12 18:47:17
 * @LastEditTime: 2022-10-08 19:26:34
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description: 新建 / 编辑 页面
 * @FilePath: \vue3-element-plus-web\src\views\SysManage\Resource\components\Drawer\create.vue
 * Copyright 版权声明
-->
<template>
  <div class="">
    <el-form
      :rules="rules"
      ref="dataForm"
      :model="temp"
      label-width="140px"
      style='width:100%;'
    >

      <el-form-item label="父资源名称：" v-if="pageParams.parentName">
        {{pageParams.parentName}}
      </el-form-item>

      <el-form-item label="资源类型：" prop="category">
        <el-select
          v-model="temp.category"
          placeholder="请选择资源类型"
        >
          <el-option
            v-for="item in CATEGORY_ENUM"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="资源编号：" prop="code">
        <el-input v-model="temp.code" placeholder="请输入资源编号"></el-input>
      </el-form-item>

      <el-form-item label="资源名称：" prop="name">
        <el-input v-model="temp.name" placeholder="请输入资源名称"></el-input>
      </el-form-item>

      <el-form-item label="资源别名：" prop="alias">
        <el-input v-model="temp.alias" placeholder="请输入资源别名"></el-input>
      </el-form-item>

      <template v-if="temp.category === 1">
        <el-form-item label="图标：" prop="source">
          <el-input v-model="temp.source" placeholder="请选择图标"></el-input>
        </el-form-item>
        <el-form-item label="请求地址：" prop="path">
          <el-input v-model="temp.path" placeholder="请输入请求地址"></el-input>
        </el-form-item>

        <el-form-item label="是否打开新窗口：" prop="isOpen">
          <el-radio-group v-model="temp.isOpen">
            <el-radio
              v-for="item in IS_OPEN_ENUM"
              :key="item.value"
              :label="item.value">
              {{item.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否快捷菜单：" prop="isQuick">
          <el-radio-group v-model="temp.isQuick">
            <el-radio
              v-for="item in IS_QUICK_ENUM"
              :key="item.value"
              :label="item.value">
              {{item.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <template v-if="temp.isQuick === 1">
          <el-form-item label="快捷菜单图标地址：" prop="quickIconUrl">
            <el-input v-model="temp.quickIconUrl" placeholder="请输入快捷菜单图标地址"></el-input>
          </el-form-item>

          <el-form-item label="H5菜单地址：" prop="h5Path">
            <el-input v-model="temp.h5Path" placeholder="请输入H5菜单地址"></el-input>
          </el-form-item>
        </template>
      </template>

      <el-form-item label="备注：" prop="remark">
        <el-input v-model="temp.remark" placeholder="请输入备注"></el-input>
      </el-form-item>

      <el-form-item label="序号：" prop="sort">
        <el-input v-model="temp.sort" placeholder="请输入序号"></el-input>
      </el-form-item>

      <el-form-item label="状态：" prop="status">
        <el-radio-group v-model="temp.status">
          <el-radio
            v-for="item in stateList"
            :key="item.value"
            :label="item.value">
            {{item.label}}
          </el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { ENABLED_STATE_ENUM } from '@/config'
import { positiveInteger } from '@/utils/validate'
import { CATEGORY_ENUM, IS_OPEN_ENUM, IS_QUICK_ENUM } from '@/config/views/SysManage/Resource'

export default {
  name: 'Create',
  computed: {
    ...mapGetters([
      'drawerCurrent'
    ]),
    drawerType: function() {
      return this.drawerCurrent.drawerType
    }
  },
  components: {
  },
  data() {
    // 校验正整数
    const versionNum = (rule, value, callback) => {
      value = value * 1
      if (value === 0) {
        callback()
      } else {
        if (!positiveInteger(value)) {
          callback(new Error('请输入小于11位的整数'))
        } else {
          callback()
        }
      }
    }
    return {
      pageParams: {},
      temp: {
        category: 1,
        isOpen: 2,
        isQuick: 2,
        status: 1
      },
      rules: {
        category: [
          { required: true, message: '请选择资源类型', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入资源编号', trigger: 'change' },
          { max: 50, message: '资源编号最大长度50位', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入资源名称', trigger: 'change' },
          { max: 50, message: '资源名称最大长度50位', trigger: 'change' }
        ],
        path: [
          { required: true, message: '请输入请求地址', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请输入序号', trigger: 'change' },
          { required: true, validator: versionNum, trigger: 'change' }
        ]
      },
      // 状态列表
      stateList: ENABLED_STATE_ENUM,
      // 资源类型 枚举
      CATEGORY_ENUM,
      // 是否打开新窗口 枚举
      IS_OPEN_ENUM,
      // 是否快捷菜单 枚举
      IS_QUICK_ENUM
    }
  },
  watch: {
    // 表单内容体
    temp: {
      handler: function(val, oldVal) {
      },
      deep: true
    }
  },
  created() {
    console.log(this.drawerCurrent.params)
    // 列表传过来的数据
    const temp = this.drawerCurrent.params.row
    this.temp = Object.assign({}, this.temp, temp)
    this.pageParams = this.drawerCurrent.params.pageParams || {}
    if (this.pageParams.parentId) {
      this.temp.parentId = this.pageParams.parentId
    }
    // *******************如果是 【编辑】 需要从接口数据的话执行一下操作
    if (this.drawerType === 'UPDATE') {
      // drawer loadding 状态 打开
      this.$store.dispatch('SetDrawerloading', true)
      setTimeout(() => {
        // drawer loadding 状态 关闭
        this.$store.dispatch('SetDrawerloading', false)
      }, 500)
    }
  },
  mounted() {

  },
  methods: {
    // 提交表单
    submitForm() {
      return this.$refs['dataForm'].validate()
    },
    // 获取表单数据
    getFormData() {
      return this.temp
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
